package com.ruoyi.web.controller.bank;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TAppBalance;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TAppTransaction;
import com.ruoyi.system.service.ITAppTransactionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 交易回单Controller
 * 
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/system/transaction")
public class TAppTransactionController extends BaseController
{
    @Autowired
    private ITAppTransactionService tAppTransactionService;

    /**
     * 查询交易回单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TAppTransaction tAppTransaction)
    {
        startPage();
        List<TAppTransaction> list = tAppTransactionService.selectTAppTransactionList(tAppTransaction);
        return getDataTable(list);
    }

    /**
     * 导出交易回单列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, TAppTransaction tAppTransaction)
    {
        List<TAppTransaction> list = tAppTransactionService.selectTAppTransactionList(tAppTransaction);
        ExcelUtil<TAppTransaction> util = new ExcelUtil<TAppTransaction>(TAppTransaction.class);
        util.exportExcel(response, list, "交易回单数据");
    }

    /**
     * 获取交易回单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tAppTransactionService.selectTAppTransactionById(id));
    }
    /**
     * 获取交易回单详细信息
     */
    @GetMapping(value = "/id/{id}")
    public AjaxResult getInfoById(@PathVariable("id") Long id)
    {
        return success(tAppTransactionService.getInfoById(id));
    }

    /**
     * 新增交易回单
     */
    @PostMapping
    public AjaxResult add(@RequestBody TAppTransaction tAppTransaction)
    {
        return toAjax(tAppTransactionService.insertTAppTransaction(tAppTransaction));
    }

    /**
     * 修改交易回单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TAppTransaction tAppTransaction)
    {
        return toAjax(tAppTransactionService.updateTAppTransaction(tAppTransaction));
    }

    /**
     * 删除交易回单
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tAppTransactionService.deleteTAppTransactionByIds(ids));
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception{
        ExcelUtil<TAppTransaction> util = new ExcelUtil<>(TAppTransaction.class);
        List<TAppTransaction> tAppTransactions = util.importExcel(file.getInputStream());
        for (TAppTransaction tAppTransaction : tAppTransactions) {

            if(StringUtils.isNotEmpty(tAppTransaction.getTransactionType())){
                tAppTransaction.setTransactionType(tAppTransaction.getTransactionType().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getTransactionDate())){
                tAppTransaction.setTransactionDate(tAppTransaction.getTransactionDate().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getTransactionTime())){
                tAppTransaction.setTransactionTime(tAppTransaction.getTransactionTime().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getTradeAmount())){
                tAppTransaction.setTradeAmount(tAppTransaction.getTradeAmount().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getAfterTransactionBalance())){
                tAppTransaction.setAfterTransactionBalance(tAppTransaction.getAfterTransactionBalance().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getAccountName())){
                tAppTransaction.setAccountName(tAppTransaction.getAccountName().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getAccountNumber())){
                tAppTransaction.setAccountNumber(tAppTransaction.getAccountNumber().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getBusinessType())){
                tAppTransaction.setBusinessType(tAppTransaction.getBusinessType().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppTransaction.getPurpose())){
                tAppTransaction.setPurpose(tAppTransaction.getPurpose().trim().replace("\n", ""));
            }
        }
        return AjaxResult.success(tAppTransactionService.importTAppBalances(tAppTransactions));
    }
}
